public class List <T> {
    private final Node <T> list;
    {
        //初始化个空节点，减少处理处理空链表的操作
        list = new Node<>();
        list.setNext(null);
    }
    
    private Node<T> findPrev(T aim) {
        Node<T> temp = list;
        while (temp.getNext() != null && temp.getNext().getData()!= aim) {
            temp = temp.getNext();
        }
        return temp;
    }
    
    public Node<T> find(T aim) {
        return findPrev(aim).getNext();
    }
    
    public void add(T in) {
        Node<T> prev = findPrev(in);
        //需要插入
        if (prev.getNext() == null) {
            Node <T> node = new Node<>();
            node.setData(in);
            node.setNext(prev.getNext());
            prev.setNext(node);
        }
    }
    
    public void update(T oldValue, T newValue) {
        find(oldValue).setData(newValue);
    }
    
    public void delete(T aim) {
        Node<T> prev = findPrev(aim);
        if (prev.getNext() != null) {
            prev.setNext(prev.getNext().getNext());
        }
    }
    
    public void outPut() {
        Node<T> temp = list;
        while (temp.getNext() != null) {
            System.out.println(temp.getNext().getData());
            temp = temp.getNext();
        }
    }
    
    public static void main(String[] args) {
        List<Integer> list = new List<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.outPut();
        System.out.println(list.find(1).getData());
        System.out.println(list.find(2).getData());
        System.out.println(list.find(5).getData());
        if (list.find(6) == null) {
            System.out.println("lost");
        }
        list.delete(1);
        list.outPut();
        list.delete(5);
        list.outPut();
        list.delete(3);
        list.outPut();
        list.update(2, 0);
        list.outPut();
    }
}
